<?php
$path = $_GET['path'];
$path = explode(',', $path);
$count = count($path);
$base = dirname(__FILE__);

header("Content-Type: text/javascript");
echo "// build.js.php: scanning $count paths for files... \n";
foreach($path as $folder) {
	$folder = $folder == '.' ? $base . '/' : $base . '/' . $folder . '/';
	echo "// folder: $folder \n";
	$files = array();
	if ($dh = opendir($folder)) {
		while (($file = readdir($dh)) !== false) {
        	if (pathinfo($file, PATHINFO_EXTENSION) == 'js') {
        		$files[] = $file;
        	}
		}
        closedir($dh);
		usort($files, "sortByLength");
		foreach($files as $file) {
        	echo "// file: $file \n";
        	readfile($folder.$file);
        	echo "\n\n";	
		}
    }
    else {
    	$error = print_r(error_get_last(), true);
    	echo "failed: $error \n";
    }
}

function sortByLength($string1, $string2) {
	$a = strlen($string1);
	$b = strlen($string2);
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
?>